package com.jiepos.mpos.common.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Host_LargeEntity;
import com.jiepos.mpos.core.dao.BaseDao;

/**
 * 
 * 
 * <p>标题: —— 大商户表操作类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.common.dao 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月27日下午2:15:15
 * @history（历次修订内容、修订人、修订时间等）
 */
@Repository
public class HostLargeDao extends BaseDao<App_Host_LargeEntity> {	
	
	public void saveEntity(List paramList,String tablename){
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO ");
		sql.append(tablename);
		sql.append(" (host_large_uuid,term_nm,trf_mchnt_cd,trf_term_id,ins_uuid,service_uuid,service_param_uuid,area_cd,");
		sql.append(" acq_ins_cd,master_key_idx,master_key,mac_key,pin_key,trk_key,debit_minfee,debit_rate,debit_maxfee,");
		sql.append(" credit_minfee,credit_rate,credit_maxfee,sign_st,rec_st,rec_crt_ts,rec_upd_ts,rec_upd_usr_id,last_oper_in)");
		sql.append(" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		update(sql.toString(), paramList);
	}
	
	
	public void updateEntity(List paramList,String tablename){
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(tablename);
		sql.append(" SET term_nm=?,trf_mchnt_cd=?,trf_term_id=?,ins_uuid=?,service_uuid=?,service_param_uuid=?,");
		sql.append(" area_cd=?,acq_ins_cd=?,master_key_idx=?,master_key=?,mac_key=?,pin_key=?,trk_key=?,debit_minfee=?,");
		sql.append(" debit_rate=?,debit_maxfee=?,credit_minfee=?,credit_rate=?,credit_maxfee=?,sign_st=?,");
		sql.append(" rec_upd_ts=?,rec_upd_usr_id=?,last_oper_in=? WHERE rec_id=?");
		update(sql.toString(), paramList);
	}
	
	public Map getEntityById(String tablename,String host_large_uuid){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM ");
		sql.append(tablename);
		sql.append(" where host_large_uuid='");
		sql.append(host_large_uuid).append("' and rec_st!='0'");
		return queryForMap(sql.toString());
	}
	
	public void updateStatusById(String param, String idsStr,String tablename) {
		final String[] ids = idsStr.split(",");
		String sql = " UPDATE " + tablename + " SET REC_ST='"
				+ SystemConstant.DEL_STATUS + "' , REC_UPD_TS=SYSTIMESTAMP , last_oper_in='" + SystemConstant.OPER_DEL
				+ "' WHERE " + param + " = ?";
		log.debug("deleteByStatus sql : " + sql);
		log.debug("delete by status ids: " + ids);
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

			@Override
			public int getBatchSize() {
				return ids.length;
			}

			@Override
			public void setValues(PreparedStatement arg0, int arg1) throws SQLException {
				arg0.setString(1, ids[arg1]);
			}

		});
	}

}
